from flybot.settings import PUBLIC_KEY, BASE_DIR
import paramiko, os, time

def check_env(hostname, username, password, port=22):
    # 创建 SSH 客户端
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname, port=port, username=username, password=password)

    # 检查 python3 环境
    stdin, stdout, stderr = client.exec_command('python3 --version')
    python_version_result = stdout.read().decode()
    print(python_version_result)

    stdin, stdout, stderr = client.exec_command('sudo ls -al /')
    ls_result = stdout.read().decode()

    # 断开与远程服务器的连接
    client.close()
    print(python_version_result, ls_result)

    # 输出命令结果
    if "not found" in stdout.read().decode():
        return False
    else:
        return True

def deploy_wx_gpt(hostname, username, password, config, port=22):
    print("开始部署")
    # 创建 SSH 客户端
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname, port=port, username=username, password=password)
    # 将公钥写入用户文件
    client.exec_command("echo '\n{}'>>~/.ssh/authorized_keys".format(PUBLIC_KEY))
    # 创建文件夹
    time_tag = str(int(time.time()))
    bot_path = "/home/{}/wx_gpt_{}".format(username, time_tag)
    client.exec_command('mkdir -p {}'.format(bot_path))
    time.sleep(3)
    # 将文件拷贝到远程主机
    local_path = os.path.join(BASE_DIR, "flybotClients/wx_bot")
    os.system("scp -r {}/* {}@{}:{}".format(
        local_path,
        username,
        hostname,
        bot_path
    ))
    print("文件拷贝完毕")
    time.sleep(3)
    # 安装依赖
    client.exec_command('pip3 install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com -r {}'.format(os.path.join(bot_path, "requirements.txt")))
    print("依赖安装完毕")
    # 配置 config 文件
    client.exec_command("echo '%s'>'%s'"%(
        config,
        os.path.join(bot_path, "config.json")
    ))
    print("config配置完毕")

    # 启动应用
    stdin, stdout, stderr = client.exec_command('cd {};nohup python3 app.py&'.format(bot_path))
    print("应用已启动")
    time.sleep(6)

    # 将二维码拉取到服务器指定目录
    os.system("scp {}@{}:{}/QR.png {}/QR_{}.png".format(
        username,
        hostname,
        bot_path,
        os.path.join(BASE_DIR, "static"),
        time_tag
    ))
    print("二维码已生成")

    client.close()
    return "/static/QR_{}.png".format(time_tag)

def deploy_wx_mj(hostname, username, password, config, port=22):
    print("开始部署")
    # 创建 SSH 客户端
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname, port=port, username=username, password=password)
    # 将公钥写入用户文件
    client.exec_command("echo '\n{}'>>~/.ssh/authorized_keys".format(PUBLIC_KEY))
    # 创建文件夹
    time_tag = str(int(time.time()))
    bot_path = "/home/{}/wx_mj_{}".format(username, time_tag)
    client.exec_command('mkdir -p {}'.format(bot_path))
    time.sleep(3)
    # 将文件拷贝到远程主机
    local_path = os.path.join(BASE_DIR, "flybotClients/wx_bot")
    os.system("scp -r {}/* {}@{}:{}".format(
        local_path,
        username,
        hostname,
        bot_path
    ))
    print("文件拷贝完毕")
    time.sleep(3)
    # 安装依赖
    client.exec_command('pip3 install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com -r {}'.format(os.path.join(bot_path, "requirements.txt")))
    print("依赖安装完毕")
    # 配置 config 文件
    client.exec_command("echo '%s'>'%s'"%(
        config,
        os.path.join(bot_path, "config.json")
    ))
    print("config配置完毕")

    # 启动应用
    stdin, stdout, stderr = client.exec_command('cd {};nohup python3 app.py&'.format(bot_path))
    print("应用已启动")
    time.sleep(6)

    # 将二维码拉取到服务器指定目录
    os.system("scp {}@{}:{}/QR.png {}/QR_{}.png".format(
        username,
        hostname,
        bot_path,
        os.path.join(BASE_DIR, "static"),
        time_tag
    ))
    print("二维码已生成")

    client.close()
    return "/static/QR_{}.png".format(time_tag)
